package cn.yeyiu.sexyiu.activity;

import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import cn.yeyiu.sexyiu.R;
import cn.yeyiu.sexyiu.adapter.VideoListAdapter;
import cn.yeyiu.sexyiu.listener.OnItemClickListener;
import cn.yeyiu.util.MediaUtil;
import cn.yeyiu.util.VideoUtil;
import cn.yeyiu.util.bean.VideoInfo;
import cn.yeyiu.videoplayer.VideoPlayerActivity;
import java.util.ArrayList;
import java.util.List;

public class AnimeListActivity extends AppCompatActivity {
    private ArrayAdapter<String> arrayAdapter;
    private ImageView backBtn;
    private TextView backTv;
    private VideoListAdapter listAdapter;
    private RecyclerView recyclerView;
    private MutableLiveData<List<String>> serialLd;
    private List<String> serialList;
    private Spinner serialSpin;
    private Spinner yearsSpin;

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_anime_list);
        dataInit();
        viewInit();
        adapterInit();
        eventInit();
    }

    private void dataInit() {
        this.serialLd = new MutableLiveData<>();
        this.serialList = new ArrayList<>();
        this.listAdapter = new VideoListAdapter(this);

        this.arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, this.serialList);
        this.arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    }

    private void viewInit() {
        this.recyclerView = (RecyclerView) findViewById(R.id.video_card_rv);
        this.backBtn = (ImageView) findViewById(R.id.back_btn);
        this.backTv = (TextView) findViewById(R.id.back_tv);
        this.yearsSpin = (Spinner) findViewById(R.id.years);
        this.serialSpin = (Spinner) findViewById(R.id.serials);
    }

    private void adapterInit() {
        Display defaultDisplay = getWindowManager().getDefaultDisplay();
        if (defaultDisplay.getWidth() > defaultDisplay.getHeight()) {
            this.recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        } else {
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            this.recyclerView.setLayoutManager(linearLayoutManager);
        }
        this.recyclerView.setAdapter(this.listAdapter);
        this.serialSpin.setAdapter((SpinnerAdapter) this.arrayAdapter);
        this.listAdapter.setPageSize(20);
    }

    private void eventInit() {
        this.backBtn.setOnClickListener(v->{finish();});
        this.listAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemLongClick(View view) {
            }

            @Override
            public void onItemClick(View view) {
                int childAdapterPosition = recyclerView.getChildAdapterPosition(view);
                VideoInfo videoInfo = listAdapter.videoInfoList.getValue().get(childAdapterPosition);
                MediaUtil.playVideo(AnimeListActivity.this, VideoPlayerActivity.class, videoInfo);
            }
        });
        this.listAdapter.videoInfoList.observe(this, list->{
            this.listAdapter.notifyDataSetChanged();
        });
        this.yearsSpin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long j) {
                VideoUtil.postAnimeSerialList(AnimeListActivity.this.yearsSpin.getSelectedItem().toString(), AnimeListActivity.this.serialLd);
            }
        });
        this.serialLd.observe(this, new Observer<List<String>>() {
            public void onChanged(List<String> list) {
                AnimeListActivity.this.serialList.clear();
                AnimeListActivity.this.serialList.addAll(list);
                AnimeListActivity.this.arrayAdapter.notifyDataSetChanged();
            }
        });
        this.serialSpin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }

            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long j) {
                String serial = AnimeListActivity.this.serialSpin.getSelectedItem().toString();
                AnimeListActivity.this.backTv.setText(serial);
                VideoUtil.postSerialAnimeList(serial, AnimeListActivity.this.listAdapter.videoInfoList);
            }
        });
    }
}
